<template>
  <el-dialog
    :title="!dataForm.gsmBargainId ? '添加活动' : '修改'"
    :close-on-click-modal="false"
    :append-to-body="true"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="砍价商品" prop="psmId">
      砍价商品 <img :src="goods.psmImg" alt="" style="width: 100px ; height: 100px"> <br>
      {{goods.psmTitle}} <br>
      原价:{{goods.psmPrice}} <br>
      库存:{{goods.psmStock}} <br>
      <el-button type="primary" plain @click="chooseGoods()">选择商品</el-button>
    </el-form-item>
    <el-form-item label="活动名称" prop="gsmBargainName">
      <el-input v-model="dataForm.gsmBargainName" placeholder="活动名称"></el-input>
    </el-form-item>
    <el-form-item label="活动时间" prop="gsmBargainStartTime">
      <el-date-picker
        v-model="dataForm.gsmBargainStartTime"
        type="datetime"
        value-format="yyyy-MM-dd HH:mm:ss"
        placeholder="选择日期时间">
      </el-date-picker>
      至
      <el-date-picker
        v-model="dataForm.gsmBargainEndTime"
        type="datetime"
        value-format="yyyy-MM-dd HH:mm:ss"
        placeholder="选择日期时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="每人限购数量" prop="gsmBargainLimitCount">
      <el-input v-model="dataForm.gsmBargainLimitCount" placeholder="每人限购数量"></el-input>
    </el-form-item>
    <el-form-item label="商品底价" prop="psmBargainPrice">
      <el-input v-model="dataForm.psmBargainPrice" placeholder="商品底价"></el-input>
    </el-form-item>
      <el-form-item label="商品原价" prop="gsmBargainMaxCount">
        <el-input v-model="dataForm.psmPrice" placeholder="商品原价" disabled></el-input>
      </el-form-item>
    <el-form-item label="砍价规则" prop="gsmBargainMaxCount">
     <el-radio label="0" v-model="dataForm.gsmRuleType">共<el-input v-model="dataForm.gsmBargainMaxCount" placeholder="砍价规则(共砍多少刀)"></el-input>刀,砍至底价</el-radio>
      <br>
     <el-radio label="1" v-model="dataForm.gsmRuleType">每刀最多可砍<el-input v-model="dataForm.gsmBargainMaxPrice" placeholder="砍价规则(最多砍多少钱)"></el-input>元</el-radio>
    </el-form-item>
    <el-form-item label="活动描述" prop="gsmBargainShare">
      <el-input v-model="dataForm.gsmBargainShare" placeholder="活动描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>

    <el-dialog
      title="商品列表"
      :visible.sync="dialogFormVisible"
      :append-to-body="true">
      <el-table
        ref="singleTable"
        :data="goodsList"
        highlight-current-row
        @current-change="handleCurrentChange"
        style="width: 100%">
        <el-table-column
          type="index"
          width="50">
        </el-table-column>'
        <el-table-column
          property="psmCode"
          label="商品编号"
          width="120">
        </el-table-column>
        <el-table-column
          property="psmName"
          label="商品名称"
          width="120">
        </el-table-column>
        <el-table-column
          property="psmTitle"
          label="商品标题"
          width="120">
        </el-table-column>
        <el-table-column
          property="psmImg"
          label="商品展示"
          width="120">
          <template slot-scope="scope">
            <img :src="scope.row.psmImg" alt="" style="width: 100px ; height: 100px">
          </template>
        </el-table-column>
        <el-table-column
          property="psmWeight"
          label="重量"
          width="120">
        </el-table-column>
        <el-table-column
          property="psmBrand"
          label="商品品牌"
          width="120">
        </el-table-column>
        <el-table-column
          property="psmPrice"
          label="商品价格"
          width="120">
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-dialog>

  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        dialogFormVisible:false,
        goodsList:[],
        goods:{},
        visible: false,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataForm: {
          gsmBargainId: 0,
          psmId: '',
          gsmBargainName: '',
          gsmBargainStartTime: '',
          gsmBargainEndTime: '',
          gsmBargainLimitCount: '',
          psmBargainPrice: '',
          gsmBargainStatus: '',
          gsmBargainMaxCount: '',
          gsmBargainMaxPrice: '',
          gsmBargainShare: '',
          gsmRuleType:0
        },
        dataRule: {
          psmId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          gsmBargainName: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          gsmBargainStartTime: [
            { required: true, message: '活动开始时间不能为空', trigger: 'blur' }
          ],
          gsmBargainEndTime: [
            { required: true, message: '活动结束时间不能为空', trigger: 'blur' }
          ],
          gsmBargainLimitCount: [
            { required: true, message: '每人限购数量不能为空', trigger: 'blur' }
          ],
          psmBargainPrice: [
            { required: true, message: '商品底价不能为空', trigger: 'blur' }
          ],
          gsmBargainStatus: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          gsmBargainShare: [
            { required: true, message: '活动描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getGoods();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getGoods();
      },
      getGoods() {
        this.$http({
          url: this.$http.adornUrl('/search/goods/esList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.goodsList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.goodsList = []
            this.totalPage = 0
          }
        })
      },
      handleCurrentChange(currentRow) {
        if (currentRow != null) {
          this.goods = currentRow
          this.dataForm.psmPrice = currentRow.psmPrice
          this.dataForm.psmId = currentRow.psmId
          this.dialogFormVisible = false;
        }
      },
      //打开选择商品的窗口 并且查询出当前已上架的商品
      chooseGoods() {
        //打开对话框
        this.dialogFormVisible = true
        this.getGoods();
      },
      init (id) {
        this.dataForm.gsmBargainId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.gsmBargainId) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargain/info/${this.dataForm.gsmBargainId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.psmId = data.bargain.psmId
                this.dataForm.gsmBargainName = data.bargain.gsmBargainName
                this.dataForm.gsmBargainStartTime = data.bargain.gsmBargainStartTime
                this.dataForm.gsmBargainEndTime = data.bargain.gsmBargainEndTime
                this.dataForm.gsmBargainLimitCount = data.bargain.gsmBargainLimitCount
                this.dataForm.psmBargainPrice = data.bargain.psmBargainPrice
                this.dataForm.gsmBargainStatus = data.bargain.gsmBargainStatus
                this.dataForm.gsmBargainMaxCount = data.bargain.gsmBargainMaxCount
                this.dataForm.gsmBargainMaxPrice = data.bargain.gsmBargainMaxPrice
                this.dataForm.gsmBargainShare = data.bargain.gsmBargainShare
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargain/${!this.dataForm.gsmBargainId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'gsmBargainId': this.dataForm.gsmBargainId || undefined,
                'psmId': this.dataForm.psmId,
                'gsmBargainName': this.dataForm.gsmBargainName,
                'gsmBargainStartTime': this.dataForm.gsmBargainStartTime,
                'gsmBargainEndTime': this.dataForm.gsmBargainEndTime,
                'gsmBargainLimitCount': this.dataForm.gsmBargainLimitCount,
                'psmBargainPrice': this.dataForm.psmBargainPrice,
                'gsmBargainStatus': this.dataForm.gsmBargainStatus,
                'gsmBargainJoinCount': this.dataForm.gsmBargainJoinCount,
                'gsmBargainMaxCount': this.dataForm.gsmBargainMaxCount,
                'gsmBargainMaxPrice': this.dataForm.gsmBargainMaxPrice,
                'gsmBargainShare': this.dataForm.gsmBargainShare,
                'gsmRuleType': this.dataForm.gsmRuleType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
